package net.ge.mosq.web.servlet;
import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.ge.mosq.jdo.entity.AccountItem;
import net.ge.mosq.service.ServiceFactory;

@SuppressWarnings("serial")
public class MenuServlet extends BaseServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {

//		String qq = "286861806";
//		String xmlu = "317782094";
//		HttpClient httpClient = new HttpClient();
//		httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
//		String surl = "http://static.photo.qq.com/http_staload.cgi?";
//		String xasurl = "http://xastatic.photo.qq.com/http_staload.cgi?";
//		String url = xasurl + qq + "/" + xmlu;
//		//String url = 851232434/afa473db-41bb-425d-833a-14322d3078d2";
//		GetMethod getMethod = new GetMethod(url);
//		File xmlFile = null;
//		File picFile = null;
//		FileOutputStream fileOutputStream = null;
//		try {
//			httpClient.executeMethod(getMethod);
//			xmlFile = new File("C:\\tmp\\"+System.currentTimeMillis()+".xml");
//			byte[] fileBytes = getMethod.getResponseBody();
//			fileOutputStream = new FileOutputStream(xmlFile);
//			fileOutputStream.write(fileBytes);
//		} catch (HttpException e1) {
//			// TODO Auto-generated catch block
//			e1.printStackTrace();
//		} catch (IOException e1) {
//			// TODO Auto-generated catch block
//			e1.printStackTrace();
//		}finally {
//			try {
//				fileOutputStream.close();
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		}
//		FileOutputStream picCreate = null;
//		long ctm = System.currentTimeMillis();
//		File folder = new File("C:\\"+qq);
//		if (!folder.exists()) {
//			folder.mkdir();
//		}
//		folder = new File("C:\\"+qq+"\\"+ ctm + "\\");
//		if (!folder.exists()) {
//			folder.mkdir();
//		}
//		try {
//			DocumentBuilderFactory factory = DocumentBuilderFactory
//					.newInstance();
//			DocumentBuilder builder = factory.newDocumentBuilder();
//			Document doc = builder.parse(xmlFile);
//			NodeList nl = doc.getElementsByTagName("origin_url");
//			for (int i = 0; i < nl.getLength(); i++) {
//				
//				picFile = new File("C:\\"+qq+"\\"+ ctm + "\\"+i+".jpg");
//				picCreate = new FileOutputStream(picFile);
//				GetMethod pic = new GetMethod(nl.item(i).getFirstChild().getNodeValue());
//				httpClient.executeMethod(pic);
//				picCreate.write(pic.getResponseBody());
//			}
//		} catch (Exception e) {
//			e.printStackTrace();
//		} finally {
//			try {
//				picCreate.close();
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		}
//		PersistenceManager pm = PMF.get().getPersistenceManager();
//	    String query = "select from " + AccountItem.class.getName() + " where email == 'common' and parentName='TOP'";
//	    List<AccountItem> accountItems = (List<AccountItem>) pm.newQuery(query).execute();
//	    if (accountItems.isEmpty()) {
//	    	
//	    }
//		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
//		List<Entity> entity = (List<Entity>)this.getServletContext().getAttribute(CommonUtility.Atts.DEFUALT_ACC_TEMPLATE.toString());
//		datastore.put(entity);
		
		this.doPost(req, resp);
		resp.sendRedirect("/main.jsp");
		
	}

	/* (non-Javadoc)
	 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
//		if ("addCommonItems".equals(req.getParameter("action"))) {
			@SuppressWarnings("unchecked")
			List<AccountItem> items = (List<AccountItem>) this.getServletContext().getAttribute("TemplateAccounts");
			ServiceFactory.getAccountService().addCommonItems(items);
//		}
//		this.doGet(req, resp);
	}
	
}
